package com.ruoyi.dept.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.dept.domain.bo.DeptLabPersonBo;
import com.ruoyi.dept.domain.vo.DeptLabPersonVo;
import com.ruoyi.dept.domain.DeptLabPerson;
import com.ruoyi.dept.mapper.DeptLabPersonMapper;
import com.ruoyi.dept.service.IDeptLabPersonService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 试验室人员信息Service业务层处理
 *
 * @author zxs
 * @date 2024-05-22
 */
@RequiredArgsConstructor
@Service
public class DeptLabPersonServiceImpl implements IDeptLabPersonService {

    private final DeptLabPersonMapper baseMapper;

    /**
     * 查询试验室人员信息
     */
    @Override
    public DeptLabPersonVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询试验室人员信息列表
     */
    @Override
    public TableDataInfo<DeptLabPersonVo> queryPageList(DeptLabPersonBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DeptLabPerson> lqw = buildQueryWrapper(bo);
        Page<DeptLabPersonVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询试验室人员信息列表
     */
    @Override
    public List<DeptLabPersonVo> queryList(DeptLabPersonBo bo) {
        LambdaQueryWrapper<DeptLabPerson> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DeptLabPerson> buildQueryWrapper(DeptLabPersonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DeptLabPerson> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, DeptLabPerson::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), DeptLabPerson::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getEmail()), DeptLabPerson::getEmail, bo.getEmail());
        lqw.eq(StringUtils.isNotBlank(bo.getPhonenumber()), DeptLabPerson::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSex()), DeptLabPerson::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), DeptLabPerson::getStatus, bo.getStatus());
        lqw.eq(bo.getJoinWorkDate() != null, DeptLabPerson::getJoinWorkDate, bo.getJoinWorkDate());
        lqw.eq(bo.getBirthday() != null, DeptLabPerson::getBirthday, bo.getBirthday());
        lqw.eq(StringUtils.isNotBlank(bo.getProfess()), DeptLabPerson::getProfess, bo.getProfess());
        lqw.eq(StringUtils.isNotBlank(bo.getProfessCode()), DeptLabPerson::getProfessCode, bo.getProfessCode());
        lqw.eq(bo.getProfessDate() != null, DeptLabPerson::getProfessDate, bo.getProfessDate());
        return lqw;
    }

    /**
     * 新增试验室人员信息
     */
    @Override
    public Boolean insertByBo(DeptLabPersonBo bo) {
        DeptLabPerson add = BeanUtil.toBean(bo, DeptLabPerson.class);
        add.setDeptId(LoginHelper.getDeptId());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改试验室人员信息
     */
    @Override
    public Boolean updateByBo(DeptLabPersonBo bo) {
        DeptLabPerson update = BeanUtil.toBean(bo, DeptLabPerson.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DeptLabPerson entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除试验室人员信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
